package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AddshijianDto;

import com.zzyl.dto.FenyeDto;
import com.zzyl.entity.Reservation;

import com.zzyl.entity.fneye;
import com.zzyl.enums.ReservationStatus;


import com.zzyl.mapper.CanguanyuyueMapper;
import com.zzyl.service.CanguanyuyueService;
import com.zzyl.vo.FenyeVo;
import com.zzyl.vo.TimeCountVo;
import com.zzyl.vo.shijianVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CanguanyuyueServiceImpl  implements CanguanyuyueService {
    @Autowired
    private CanguanyuyueMapper canguanyuyueMapper;

    // 最大预约数量和每天最大取消次数常量
    private static final int MAX_RESERVATION_COUNT = 100; // 示例值，请根据实际需求设置
    private static final int MAX_CANCEL_COUNT = 3; // 示例值，请根据实际需求设置



    @Override
    public int chanxun9(Long memberId) {

        Integer status = ReservationStatus.CANCELED.getOrdinal();
        LocalDateTime start = LocalDateTimeUtil.beginOfDay(LocalDateTime.now());
        LocalDateTime end = LocalDateTimeUtil.endOfDay(LocalDateTime.now());

        // 在这里调用 ReservationMapper 的方法来获取取消预约次数
        return canguanyuyueMapper.chanxun9(memberId, status, start, end);
    }

    @Override
    public List<shijianVo> getcountByTime(Long time) {
           // 调用 countByTime 获取预约数量
        List<TimeCountVo> timeCountVoList =canguanyuyueMapper.countByTime(time);

        // 将 TimeCountVo 转换为 shijianVo
        List<shijianVo> shijianVoList = new ArrayList<>();
        for (TimeCountVo timeCountVo : timeCountVoList) {
            shijianVo shijianVo = new shijianVo();
            shijianVo.setTime(timeCountVo.getTime());
            shijianVo.setCount(timeCountVo.getCount());
            shijianVoList.add(shijianVo);
        }

        return shijianVoList;
    }

    @Override
    public void add(AddshijianDto dto) {
    /*    //防止重复预约：同一手机号在一个时间段内只能预约一次
        Reservation reservation = canguanyuyueMapper.SelectTime(dto.getMobile(), dto.getTime(), ReservationStatus.RESERVED.getOrdinal());

        if (reservation != null) {
            throw new RuntimeException("该时间段内您已预约，请勿重复预约");
        }
        // 控制预约数量：参观预约在某个时间段内的预约数量有上限
        ReservationType reservationType = ReservationType.getByValue(dto.getType());

        if (reservationType == ReservationType.CAN_GUAN) {
            List<TimeCountVo> timeCountVos = canguanyuyueMapper.countByTime(dto.getTime(), dto.getTime().plusMinutes(29));
            if (!CollectionUtils.isEmpty(timeCountVos) && timeCountVos.get(0).getCount() >= MAX_RESERVATION_COUNT) {
                throw new BaseException(BasicEnum.RESERVATION_COUNT_OVER);
            }
        }

        // 控制取消次数：在某个时间段内只能取消预约次数有上限
        LocalDateTime start = LocalDateTimeUtil.beginOfDay(LocalDateTime.now());
        LocalDateTime end = LocalDateTimeUtil.endOfDay(LocalDateTime.now());

        Integer cancelCount = canguanyuyueMapper.chanxun(dto.getMemberId(), ReservationStatus.CANCELED.getOrdinal(), start, end);

        if (cancelCount >= MAX_CANCEL_COUNT) {
            throw new BaseException(BasicEnum.RESERVATION_COUNT_OVER);
        }
*/

        // 保存预约信息：所有条件满足后，将新的预约信息保存到数据库中
        Reservation insert = BeanUtil.copyProperties(dto, Reservation.class);
        insert.setStatus(ReservationStatus.PENDING.getOrdinal());
        canguanyuyueMapper.insert(insert);


    }

    @Override
    public PageResponse getPage(FenyeDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<FenyeVo> list = canguanyuyueMapper.getPage(dto);
        return PageResponse.of(list);

    }

    @Override
    public void cancel(Long id) {

        // 查询预约信息
        Reservation reservation = canguanyuyueMapper.selectById(id);

        if (reservation == null) {
            throw new RuntimeException("预约信息不存在");
        }

        // 判断预约状态是否为已预约
        if (reservation.getStatus() != ReservationStatus.RESERVED.getOrdinal()) {
            throw new RuntimeException("预约状态不是已预约，无法取消");
        }

        // 更新预约状态为已取消
        reservation.setStatus(ReservationStatus.CANCELED.getOrdinal());
        canguanyuyueMapper.update(reservation);


    }
}
